package net.zjitc.service;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import net.zjitc.dao.BillDao;
import net.zjitc.dao.CustomerDao;
import net.zjitc.dao.RuleDao;
import net.zjitc.pojo.Bill;
import net.zjitc.pojo.Customer;
import net.zjitc.pojo.Rule;
import net.zjitc.pojo.VipRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DiscountService {

    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private RuleDao ruleDao;
    @Autowired
    private BillDao billDao;

    public Customer getCustomer(String idCard){
        return customerDao.findByIdCard(idCard);
    }
    public Customer getCustomerById(long id){
        return customerDao.findById(id);
    }
    public Bill updateBillTotalPrice(Bill bill){
        return billDao.updateBillTotalPrice(bill);
    }

    public Object getVipLevel(Object o){
        return o;
    }

    public List<Bill> findByCustomerId(Long customerId){
        return billDao.findByCustomerId(customerId);
    }

    public BigDecimal discount(String vipLevel,String itemName,BigDecimal price) throws Exception {
        VipRule vipRule= ruleDao.findRulesByLevel(vipLevel);
        List<Rule> ruleList=ruleDao.findRulesByVipRuleId(vipRule.getId());
        BigDecimal currentPrice=price;
        for (Rule rule:ruleList){
            ExpressRunner runner = new ExpressRunner();
            DefaultContext<String, Object> context = new DefaultContext<String, Object>();
            context.put("价格",price);
            String express=rule.getExpress();
            Object r = runner.execute(express, context, null, true, false);
            currentPrice=BigDecimal.valueOf(Double.valueOf(String.valueOf(r)));

        }
        return currentPrice;
    }
}
